package com.example.selfservicestation.base

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.navigation.fragment.findNavController
import androidx.viewbinding.ViewBinding
import com.example.selfservicestation.activity.MainActivity
import com.example.selfservicestation.databinding.FragmentBaseBinding
import com.example.selfservicestation.util.LOG_TAG


abstract class BaseBindingFragment2<VB : ViewBinding>: Fragment(){

    private var _baseBinding: FragmentBaseBinding? = null
    private val baseBinding
        get() = _baseBinding!!

    private var _binding: VB? = null
    protected val binding: VB
        get() = _binding!!


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        printNameForSubclass()
    }
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        _baseBinding = FragmentBaseBinding.inflate(inflater, container, false)
        _binding = getViewBinding(inflater, baseBinding.fragmentContent)
        return baseBinding.root
    }


    private fun printNameForSubclass(){
//        Log.i(LOG_TAG,javaClass.simpleName)
    }

    private fun navigationUp(){
        findNavController().popBackStack()
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _baseBinding = null
        _binding = null
        Log.i(LOG_TAG, "onDestoryView--${javaClass.simpleName}")
    }

    fun showLoading(){
        (requireActivity() as MainActivity).showLoading()
    }

    fun dismissLoading(){
        (requireActivity() as MainActivity).dismissLoading()
    }

    protected abstract fun getViewBinding(inflater: LayoutInflater, container: ViewGroup): VB
}